//
//  TwitterParser.m
//  TuiTe
//
//  Created by 张 霄鹤 on 10-3-17.
//  Copyright 2010 xhzhang. All rights reserved.
//

#import "TwitterParser.h"
#import "DataManager.h"

#import "DocumentBuilder.h"
#import "Status.h"
#import "Tweet.h"


@implementation TwitterParser

+ (NSArray *)parseUsers:(NSString *)xml{
#ifdef DEBUG
	NSLog(@"xml = %@",xml);
#endif
	NSMutableArray *ret = [NSMutableArray array];
	DocumentBuilder *builder = [[DocumentBuilder alloc] init];
	Document *doc = [builder parseWithString:xml];
	[builder release];
	Element *re = [doc rootElement];
	NSArray *els = [re getElementsByName:@"user"];
	if(els){
		User1 *usr;
		for(Element *el in els){
			// status = [[Tweet alloc] init];
			usr = [[User1 alloc] init];
			[usr parse:el];
			[ret addObject:usr];
			[usr release];
		}
	}else{
#ifdef DEBUG
		NSLog(@"Empty");
#endif
		// TODO 此处要进行内容处理
	}
	return ret;
}

+ (User1 *)parseUser:(NSString *)xml{
	User1 *ret = nil;
	DocumentBuilder *builder = [[DocumentBuilder alloc] init];
	Document *doc = [builder parseWithString:xml];
	[builder release];
	Element *re = [doc rootElement];
	ret = [[[User1 alloc] init] autorelease];
	[ret parse:re];
	return ret;
}


+ (NSArray *)parseFriendshipIDs:(NSString *)xml{
	NSMutableArray *ret = nil;
	
	if(xml){
		DocumentBuilder *builder = [[DocumentBuilder alloc] init];
		Document *doc = [builder parseWithString:xml];
		[builder release];
		Element *re = [doc rootElement];		
		ret = [NSMutableArray array];
		
		Node *nd = [re getChildByName:@"next_cursor"];
		if(nd){
			NSString *ns = [[nd getChildByName:@"#TEXT"] getValue];
			if(ns){
				[ret addObject:ns];
			}else{
				[ret addObject:@"-1"];
			}
		}else{
			[ret addObject:@"-1"];
		}
		
		nd = [re getChildByName:@"previous_cursor"];
		if(nd){
			NSString *ns = [[nd getChildByName:@"#TEXT"] getValue];
			if(ns){
				[ret addObject:ns];
			}else{
				[ret addObject:@"-1"];
			}
		}else{
			[ret addObject:@"-1"];
		}
		
		nd = [re getChildByName:@"ids"];
		if(nd){
			re = (Element *)nd;
			NSArray *arr = [re getElementsByName:@"id"];
			if(arr){
				NSString *en;
				for(Element *el in arr){
					en = [[el getChildByName:@"#TEXT"] getValue];
					[ret addObject:en];
				}
			}
		}
	}	
	return ret;
}

+ (NSArray *)parseSearchATOM:(NSString *)xml{
	NSMutableArray *ret = nil;
	
	if(xml){
		DocumentBuilder *builder = [[DocumentBuilder alloc] init];
		Document *doc = [builder parseWithString:xml];
		[builder release];
		Element *re = [doc rootElement];		
		ret = [NSMutableArray array];
		NSArray *arr = [re getElementsByName:@"entry"];
		if(arr){
			Entry *en;
			for(Element *el in arr){
				en = [[Entry alloc] init];
				[en parse:el];
				[ret addObject:en];
				[en release];
			}
		}
	}	
	return ret;
}

+ (NSArray *)parseStatuses:(NSString *)xml{
//#ifdef DEBUG
//	NSLog(@"xml = %@",xml);
//#endif
	DataManager *dm = [DataManager sharedManager];
	NSManagedObjectContext *context = dm.managedObjectContext;
	NSMutableArray *ret = [NSMutableArray array];
	DocumentBuilder *builder = [[DocumentBuilder alloc] init];
	Document *doc = [builder parseWithString:xml];
	[builder release];
	Element *re = [doc rootElement];
	NSArray *els = [re getElementsByName:@"status"];
	if(els){
		Tweet *status;
		for(Element *el in els){
			// status = [[Tweet alloc] init];
			status = (Tweet *)[NSEntityDescription insertNewObjectForEntityForName:@"Tweet" inManagedObjectContext:context];
			[status parse:el];
			[ret addObject:status];
		}
	}else{
#ifdef DEBUG
		NSLog(@"Empty");
#endif
		// TODO 此处要进行内容处理
	}
	return ret;
}

+ (NSArray *)parseStatusesNotWithCoreData:(NSString *)xml{
	NSMutableArray *ret = [NSMutableArray array];
	DocumentBuilder *builder = [[DocumentBuilder alloc] init];
	Document *doc = [builder parseWithString:xml];
	[builder release];
	Element *re = [doc rootElement];
	NSArray *els = [re getElementsByName:@"status"];
	if(els){
		Status *status;
		for(Element *el in els){
			status = [[Status alloc] init];
			[status parse:el];
			[ret addObject:status];
			[status release];
		}
	}else{
#ifdef DEBUG
		NSLog(@"Empty");
#endif
		// TODO 此处要进行内容处理
	}
	return ret;	
}

@end
